﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace helicopterFight
{
    class IngameMenuState : State
    {
        private String nazwa = "IngameMenu";
        SpriteFont font1 = null;
        Game1 game = null;
        Texture2D tex2, play_button;
        KeyboardState oldKeybState;
        int buttonUp,
            changeScreen = 0; //0 - main, 1 - sound, 2- controls

        const int NUMBER_OF_BUTTONS = 8,
                  RESUME_BUTTON = 0,
                  SOUND_BUTTON = 1,
                  CONTROLS_BUTTON = 2,
                  EXIT_BUTTON = 3,
                  SFX_BUTTON = 4,
                  MENUMUSIC_BUTTON = 5,
                  GAMEMUSIC_BUTTON = 6,
                  FULLSCREEN_BUTTON = 7,

                  VOLUME_1 = 0,
                  VOLUME_2 = 1,
                  VOLUME_3 = 2,
                  VOLUME_4 = 3,
                  VOLUME_5 = 4,

                  CON_ENGINE = 0,
                  CON_FLYUP = 1,
                  CON_FLYDOWN = 2,
                  CON_FLYFORWARD = 3,
                  CON_FLYBACKWARD = 4,
                  CON_FLYLEFT = 5,
                  CON_FLYRIGHT = 6,
                  CON_SHOOT = 7,

                  MENU_BUTTON_WIDTH = 140,
                  MENU_BUTTON_HEIGHT = 59;

        Texture2D[] button_tex = new Texture2D[NUMBER_OF_BUTTONS];
        Rectangle[] button_rectangle = new Rectangle[NUMBER_OF_BUTTONS];
        Color[] button_color = new Color[NUMBER_OF_BUTTONS]; //kolory 
        
        Texture2D[] volume_tex = new Texture2D[5];
        Color[] volumeSound_color = new Color[5];
        Color[] volumeMMusic_color = new Color[5]; // menu music
        Color[] volumeGMusic_color = new Color[5]; //game music

        Texture2D[] controls_tex = new Texture2D[8];
        Color[] controls_color = new Color[8];

        public void setContent(Game1 gam)
        {
            game = gam;
        }

        public String getNazwa()
        {
            return nazwa;
        }

        public void loadContent()
        {
            tex2 = game.Content.Load<Texture2D>("bgr/flame2");
            play_button = game.Content.Load<Texture2D>("bgr/play");

            font1 = game.Content.Load<SpriteFont>("fonts/SpriteFont1");

            button_tex[RESUME_BUTTON] = game.Content.Load<Texture2D>("bgr/resume");
            button_tex[SOUND_BUTTON] = game.Content.Load<Texture2D>("bgr/sound");
            button_tex[CONTROLS_BUTTON] = game.Content.Load<Texture2D>("bgr/controls");
            button_tex[EXIT_BUTTON] = game.Content.Load<Texture2D>("bgr/exit");
            button_tex[SFX_BUTTON] = game.Content.Load<Texture2D>("bgr/sfx");
            button_tex[MENUMUSIC_BUTTON] = game.Content.Load<Texture2D>("bgr/menumusic");
            button_tex[GAMEMUSIC_BUTTON] = game.Content.Load<Texture2D>("bgr/gamemusic");
            button_tex[FULLSCREEN_BUTTON] = game.Content.Load<Texture2D>("bgr/fullscreen");

            volume_tex[VOLUME_1] = game.Content.Load<Texture2D>("bgr/volume_1_10x20");
            volume_tex[VOLUME_2] = game.Content.Load<Texture2D>("bgr/volume_2_10x30");
            volume_tex[VOLUME_3] = game.Content.Load<Texture2D>("bgr/volume_3_10x40");
            volume_tex[VOLUME_4] = game.Content.Load<Texture2D>("bgr/volume_4_10x50");
            volume_tex[VOLUME_5] = game.Content.Load<Texture2D>("bgr/volume_5_10x60");

            controls_tex[CON_ENGINE] = game.Content.Load<Texture2D>("bgr/con_engine");
            controls_tex[CON_FLYUP] = game.Content.Load<Texture2D>("bgr/con_flyup");
            controls_tex[CON_FLYDOWN] = game.Content.Load<Texture2D>("bgr/con_flydown");
            controls_tex[CON_FLYFORWARD] = game.Content.Load<Texture2D>("bgr/con_flyforward");
            controls_tex[CON_FLYBACKWARD] = game.Content.Load<Texture2D>("bgr/con_flybackward");
            controls_tex[CON_FLYLEFT] = game.Content.Load<Texture2D>("bgr/con_flyleft");
            controls_tex[CON_FLYRIGHT] = game.Content.Load<Texture2D>("bgr/con_flyright");
            controls_tex[CON_SHOOT] = game.Content.Load<Texture2D>("bgr/con_shoot");
        }

        public void initialize()
        {
            buttonUp =  RESUME_BUTTON;

            int x = game.graphics.PreferredBackBufferWidth / 2 - (MENU_BUTTON_WIDTH / 2);
            int y = game.graphics.PreferredBackBufferHeight / 2 - NUMBER_OF_BUTTONS / 2 * MENU_BUTTON_HEIGHT - (NUMBER_OF_BUTTONS % 2) * MENU_BUTTON_HEIGHT / 2;
            y -= 400;
            for (int i = 0; i < NUMBER_OF_BUTTONS; i++)
            {

                button_color[i] = Color.White;
                button_rectangle[i] = new Rectangle(x, y, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
                y += MENU_BUTTON_HEIGHT;
            }

            for (int i = 0; i < NUMBER_OF_BUTTONS; i++)
            {
                button_color[i] = Color.White;
                button_rectangle[i] = new Rectangle(x, y, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
                y += MENU_BUTTON_HEIGHT;
            }

            for (int i = 0; i < 8; i++)
                controls_color[i] = Color.White;

            for (int i = 0; i < 5; i++)
            {
                if (i <= MenuState.volumeLevelSound)
                    volumeSound_color[i] = Color.Red;
                else
                    volumeSound_color[i] = Color.White;
            }

            for (int i = 0; i < 5; i++)
            {
                if (i <= MenuState.volumeLevelMMusic)
                    volumeMMusic_color[i] = Color.Red;
                else
                    volumeMMusic_color[i] = Color.White;
            }

            for (int i = 0; i < 5; i++)
            {
                if (i <= MenuState.volumeLevelGMusic)
                    volumeGMusic_color[i] = Color.Red;
                else
                    volumeGMusic_color[i] = Color.White;
            }

            button_color[RESUME_BUTTON] = Color.Yellow;
            oldKeybState = Keyboard.GetState();
           
        }



        public void update()
        {
            KeyboardState ks = Keyboard.GetState();
            if (ks.IsKeyDown(Keys.Down) && !oldKeybState.IsKeyDown(Keys.Down))
            {
                if (changeScreen == 0)
                {
                    if (buttonUp == RESUME_BUTTON)
                    {
                        MenuState.soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = SOUND_BUTTON;
                        button_color[buttonUp] = Color.Yellow;

                    }
                    else if (buttonUp == SOUND_BUTTON)
                    {
                        MenuState.soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = CONTROLS_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == CONTROLS_BUTTON)
                    {
                        MenuState.soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = EXIT_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                }

                if (changeScreen == 1)
                {
                    if (buttonUp == SFX_BUTTON)
                    {
                        MenuState.soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = MENUMUSIC_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == MENUMUSIC_BUTTON)
                    {
                        MenuState.soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = GAMEMUSIC_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == GAMEMUSIC_BUTTON)
                    {
                        MenuState.soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = EXIT_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                }

            }
            else if (ks.IsKeyDown(Keys.Up) && !oldKeybState.IsKeyDown(Keys.Up))
            {
                if (changeScreen == 0)
                {
                    if (buttonUp == SOUND_BUTTON)
                    {
                        MenuState.soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = RESUME_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == CONTROLS_BUTTON)
                    {
                        MenuState.soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = SOUND_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == EXIT_BUTTON)
                    {
                        MenuState.soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = CONTROLS_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                }

                if (changeScreen == 1)
                {
                    if (buttonUp == EXIT_BUTTON)
                    {
                        MenuState.soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = GAMEMUSIC_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == GAMEMUSIC_BUTTON)
                    {
                        MenuState.soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = MENUMUSIC_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                    else if (buttonUp == MENUMUSIC_BUTTON)
                    {
                        MenuState.soundBank.PlayCue("switch");
                        button_color[buttonUp] = Color.White;
                        buttonUp = SFX_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                    }
                }
            }

            else if (ks.IsKeyDown(Keys.Left) && !oldKeybState.IsKeyDown(Keys.Left)) 
            {
                if (changeScreen == 1)
                {
                    if (MenuState.volumeLevelSound > 0 && buttonUp == SFX_BUTTON)
                    {
                        volumeSound_color[MenuState.volumeLevelSound] = Color.White;
                        MenuState.volumeLevelSound--;
                        MenuState.soundVolume -= 2.5f;
                        MenuState.soundCategory.SetVolume(MenuState.soundVolume);
                    }

                    if (MenuState.volumeLevelMMusic > 0 && buttonUp == MENUMUSIC_BUTTON)
                    {
                        volumeMMusic_color[MenuState.volumeLevelMMusic] = Color.White;
                        MenuState.volumeLevelMMusic--;
                        MenuState.musicVolume -= 2.0f;
                        MenuState.musicCategory.SetVolume(MenuState.musicVolume);
                    }

                    if (MenuState.volumeLevelGMusic > 0 && buttonUp == GAMEMUSIC_BUTTON)
                    {
                        volumeGMusic_color[MenuState.volumeLevelGMusic] = Color.White;
                        MenuState.volumeLevelGMusic--;
                        MenuState.gameVolume -= 2.0f;
                        MenuState.gamemusicCategory.SetVolume(MenuState.gameVolume);
                    }
                }
            }            

            else if (ks.IsKeyDown(Keys.Right) && !oldKeybState.IsKeyDown(Keys.Right))
            {
                if (changeScreen == 1)
                {
                    if (MenuState.volumeLevelSound < 4 && buttonUp == SFX_BUTTON)
                    {
                        MenuState.volumeLevelSound++;
                        volumeSound_color[MenuState.volumeLevelSound] = Color.Red;
                        MenuState.soundVolume += 2.5f;
                        MenuState.soundCategory.SetVolume(MenuState.soundVolume);
                    }

                    if (MenuState.volumeLevelMMusic < 4 && buttonUp == MENUMUSIC_BUTTON)
                    {
                        MenuState.volumeLevelMMusic++;
                        volumeMMusic_color[MenuState.volumeLevelMMusic] = Color.Red;
                        MenuState.musicVolume += 2.0f;
                        MenuState.musicCategory.SetVolume(MenuState.musicVolume);
                    }

                    if (MenuState.volumeLevelGMusic < 4 && buttonUp == GAMEMUSIC_BUTTON)
                    {
                        MenuState.volumeLevelGMusic++;
                        volumeGMusic_color[MenuState.volumeLevelGMusic] = Color.Red;
                        MenuState.gameVolume += 2.0f;
                        MenuState.gamemusicCategory.SetVolume(MenuState.gameVolume);
                    }
                }
            }

            else if (ks.IsKeyDown(Keys.Enter) && !oldKeybState.IsKeyDown(Keys.Enter))
            {
                switch (buttonUp)
                {
                    case RESUME_BUTTON: MenuState.soundBank.PlayCue("badbang");
                        game.sm.zmienStan("Sandbox");  
                        MenuState.bgrMusic.Pause();
                        MenuState.gameMusic.Resume();
                        if (TestState.heliCue.IsPaused)
                            TestState.heliCue.Resume();
                        break;

                    case SOUND_BUTTON: MenuState.soundBank.PlayCue("badbang");
                        changeScreen = 1;
                        button_color[buttonUp] = Color.White;
                        buttonUp = SFX_BUTTON;
                        button_color[buttonUp] = Color.Yellow;
                        break;
                    case CONTROLS_BUTTON: changeScreen = 2;
                                            button_color[buttonUp] = Color.White;
                                            buttonUp = EXIT_BUTTON;
                                            button_color[buttonUp] = Color.Yellow;
                                          break;

                    case EXIT_BUTTON: if (changeScreen == 0)
                            game.Exit();
                        else
                            changeScreen = 0;
                        break;
                }


            }

            oldKeybState = ks;
        }
        
        public void drawSound()
        {
            game.spriteBatch.Draw(button_tex[EXIT_BUTTON], new Rectangle(600, 515, 140, 59), button_color[EXIT_BUTTON]);


            game.spriteBatch.Draw(button_tex[SFX_BUTTON], new Rectangle(190, 145, 140, 59), button_color[SFX_BUTTON]);
            game.spriteBatch.Draw(volume_tex[VOLUME_1], new Rectangle(400, 190, 7, 10), volumeSound_color[VOLUME_1]);
            game.spriteBatch.Draw(volume_tex[VOLUME_2], new Rectangle(410, 180, 7, 20), volumeSound_color[VOLUME_2]);
            game.spriteBatch.Draw(volume_tex[VOLUME_3], new Rectangle(420, 170, 7, 30), volumeSound_color[VOLUME_3]);
            game.spriteBatch.Draw(volume_tex[VOLUME_4], new Rectangle(430, 160, 7, 40), volumeSound_color[VOLUME_4]);
            game.spriteBatch.Draw(volume_tex[VOLUME_5], new Rectangle(440, 150, 7, 50), volumeSound_color[VOLUME_5]);

            game.spriteBatch.Draw(button_tex[MENUMUSIC_BUTTON], new Rectangle(190, 245, 140, 59), button_color[MENUMUSIC_BUTTON]);
            game.spriteBatch.Draw(volume_tex[VOLUME_1], new Rectangle(400, 290, 7, 10), volumeMMusic_color[VOLUME_1]);
            game.spriteBatch.Draw(volume_tex[VOLUME_2], new Rectangle(410, 280, 7, 20), volumeMMusic_color[VOLUME_2]);
            game.spriteBatch.Draw(volume_tex[VOLUME_3], new Rectangle(420, 270, 7, 30), volumeMMusic_color[VOLUME_3]);
            game.spriteBatch.Draw(volume_tex[VOLUME_4], new Rectangle(430, 260, 7, 40), volumeMMusic_color[VOLUME_4]);
            game.spriteBatch.Draw(volume_tex[VOLUME_5], new Rectangle(440, 250, 7, 50), volumeMMusic_color[VOLUME_5]);

            game.spriteBatch.Draw(button_tex[GAMEMUSIC_BUTTON], new Rectangle(190, 345, 140, 59), button_color[GAMEMUSIC_BUTTON]);
            game.spriteBatch.Draw(volume_tex[VOLUME_1], new Rectangle(400, 390, 7, 10), volumeGMusic_color[VOLUME_1]);
            game.spriteBatch.Draw(volume_tex[VOLUME_2], new Rectangle(410, 380, 7, 20), volumeGMusic_color[VOLUME_2]);
            game.spriteBatch.Draw(volume_tex[VOLUME_3], new Rectangle(420, 370, 7, 30), volumeGMusic_color[VOLUME_3]);
            game.spriteBatch.Draw(volume_tex[VOLUME_4], new Rectangle(430, 360, 7, 40), volumeGMusic_color[VOLUME_4]);
            game.spriteBatch.Draw(volume_tex[VOLUME_5], new Rectangle(440, 350, 7, 50), volumeGMusic_color[VOLUME_5]);
        }

        public void drawControls()
        {
            int y = game.graphics.PreferredBackBufferHeight / 2 - NUMBER_OF_BUTTONS / 2 * MENU_BUTTON_HEIGHT - (NUMBER_OF_BUTTONS % 2) * MENU_BUTTON_HEIGHT / 2;

            game.spriteBatch.Draw(controls_tex[CON_ENGINE], new Rectangle(200, y, 140, 30), controls_color[CON_ENGINE]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.EngineStart.ToString(), new Vector2(370, y + 8), Color.Black);
            y += 35;

            game.spriteBatch.Draw(controls_tex[CON_FLYUP], new Rectangle(200, y, 140, 30), controls_color[CON_FLYUP]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.FlyUp.ToString(), new Vector2(370, y + 8), Color.Black);
            y += 35;
            game.spriteBatch.Draw(controls_tex[CON_FLYDOWN], new Rectangle(200, y, 140, 30), controls_color[CON_FLYDOWN]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.FlyDown.ToString(), new Vector2(370, y + 8), Color.Black);
            y += 35;
            game.spriteBatch.Draw(controls_tex[CON_FLYFORWARD], new Rectangle(200, y, 140, 30), controls_color[CON_FLYFORWARD]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.FlyForward.ToString(), new Vector2(370, y + 8), Color.Black);
            y += 35;
            game.spriteBatch.Draw(controls_tex[CON_FLYBACKWARD], new Rectangle(200, y, 140, 30), controls_color[CON_FLYBACKWARD]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.FlyBackward.ToString(), new Vector2(370, y + 8), Color.Black);
            y += 35;
            game.spriteBatch.Draw(controls_tex[CON_FLYLEFT], new Rectangle(200, y, 140, 30), controls_color[CON_FLYLEFT]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.FlyLeft.ToString(), new Vector2(370, y + 8), Color.Black);
            y += 35;
            game.spriteBatch.Draw(controls_tex[CON_FLYRIGHT], new Rectangle(200, y, 140, 30), controls_color[CON_FLYRIGHT]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.FlyRight.ToString(), new Vector2(370, y + 8), Color.Black);
            y += 35;
            game.spriteBatch.Draw(controls_tex[CON_SHOOT], new Rectangle(200, y, 140, 30), controls_color[CON_SHOOT]);
            game.spriteBatch.DrawString(font1, Game1.sterowanie.ShootMissile.ToString(), new Vector2(370, y + 8), Color.Black);
            y += 100;

            game.spriteBatch.Draw(button_tex[EXIT_BUTTON], new Rectangle(200, y, 140, 59), button_color[EXIT_BUTTON]);
            //game.spriteBatch.Draw(button_tex[REDEFINE_BUTTON], new Rectangle(300, y, 140, 59), button_color[REDEFINE_BUTTON]);

        }

        public void draw(GameTime gameTime)
        {
            game.spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
            game.spriteBatch.Draw(tex2, new Rectangle(0, 0, game.graphics.PreferredBackBufferWidth, game.graphics.PreferredBackBufferHeight), Color.White);
            if (changeScreen == 0)
            {
                for (int i = 0; i < 4; i++)
                    game.spriteBatch.Draw(button_tex[i], button_rectangle[i], button_color[i]);
            }
            
            if (changeScreen == 1)
            {
                drawSound(); 
            }

            if (changeScreen == 2) 
            {
                drawControls();
            }
            game.spriteBatch.End();
        }
    }
}
